import 'package:basic_common/common_exports.dart';
import 'package:flutter_svg/flutter_svg.dart';

/* 
 * @description:下拉菜单按钮组件
 * @author: Jane 
 * @date: 2023-09-13 16:52:19
 */
class SelectWidget extends StatefulWidget {
  // 显示的菜单项
  final List<MenuItem> items;
  // 当前选中的值
  final dynamic value;
  // 选择框前的标题
  final String? title;
  // 提示语
  final String tooltip;
  String? package;
  // 选中数据的回调事件
  final ValueChanged<MenuItem>? valueChanged;
  final Widget clickChild;
   SelectWidget(
      {Key? key,
      this.items = const [],
      this.value,
      this.valueChanged,
      this.title,
      this.tooltip = "点击选择",
      this.package,
      required this.clickChild})
      : super(key: key);

  @override
  State<SelectWidget> createState() => _SelectWidgetState();
}

class _SelectWidgetState extends State<SelectWidget> {
  String label = '请选择';
  // 是否展开下拉按钮
  bool isExpand = false;
  // 当前的值
  dynamic currentValue;
  String? package;

  @override
  void initState() {
    currentValue = widget.value;
    package=widget.package;
    super.initState();
  }

  /// 根据当前的value处理当前文本显示
  void initTitle() {
    if (currentValue != null) {
      // 有值查值
      for (MenuItem item in widget.items) {
        if (item.value == currentValue) {
          label = item.label;
          return;
        }
      }
    }
    // 没值默认取第一个
    if (widget.items.isNotEmpty) {
      label = widget.items[0].label;
    }
  }

  @override
  Widget build(BuildContext context) {
    initTitle();
    return Wrap(
      children: [
        if (widget.title != null)
          Text(widget.title!, style: TextStyle(fontSize: 18)),
        PopupMenuButton<MenuItem>(
          // initialValue: currentValue,
          tooltip: widget.tooltip,
          offset: const Offset(25, 30),
          enableFeedback: true,
          child: Listener(
            // 使用listener事件能够继续传递
            onPointerDown: (event) {
              setState(() {
                isExpand = !isExpand;
              });
            },
            child: widget.clickChild,
          ),
          onSelected: (value) {
            widget.valueChanged?.call(value);
            setState(() {
              currentValue = value.value;
              isExpand = !isExpand;
            });
          },
          onCanceled: () {
            // 取消展开
            setState(() {
              isExpand = false;
            });
          },
          itemBuilder: (context) {
            return widget.items
                .map(
                  (item) => item.value == currentValue
                      ? PopupMenuItem<MenuItem>(
                          value: item,
                          child: Row(
                            children: [
                              SvgPicture.asset(
                                item.icon,
                                width: ScreenHelper.width(16),
                                height: ScreenHelper.width(16),
                                package: package,
                              ),
                              SizedBox(
                                width: ScreenHelper.width(6),
                              ),
                              Text(
                                item.label,
                                style: TextStyle(color: item.color),
                              )
                            ],
                          ),
                        )
                      : PopupMenuItem<MenuItem>(
                          value: item,
                          child: Row(
                            children: [
                              SvgPicture.asset(
                                item.icon,
                                width: ScreenHelper.width(16),
                                height: ScreenHelper.width(16),
                                package: package,
                              ),
                              SizedBox(
                                width: ScreenHelper.width(6),
                              ),
                              Text(
                                item.label,
                                style: TextStyle(color: item.color),
                              )
                            ],
                          ),
                        ),
                )
                .toList();
          },
        )
      ],
    );
  }
}

/// author baorant
/// 通用菜单项
class MenuItem {
  // 显示的文本
  String label;
  String icon;
  Color color;
  // 选中的值
  dynamic value;
  // 是否选中
  bool checked;

  MenuItem(
      {required this.label,
      required this.value,
      this.checked = false,
      required this.color,
      required this.icon});
}
